home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / mus / play / tracker_4_31.lzh / tracker / Amiga / popen.c < prev    next >
C/C++ Source or Header  |  1995-02-14  |  2KB  |  105 lines

  1. /* amiga/popen.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: popen.c,v 1.8 1995/02/14 16:51:22 espie Exp espie $
  6.  * $Log: popen.c,v $
  7.  * Revision 1.8  1995/02/14  16:51:22  espie
  8.  * *** empty log message ***
  9.  *
  10.  * Revision 1.7  1995/01/13  13:31:35  espie
  11.  * *** empty log message ***
  12.  *
  13.  * Revision 1.5  1994/01/07  15:08:54  Espie
  14.  * Maybe correct code now...
  15.  */
  16.  
  17.  
  18. #include <proto/dos.h>
  19. #include <proto/exec.h>
  20. #include <exec/tasks.h>
  21. #include <dos/dostags.h>
  22. #include "defs.h"
  23. ID("$Id: popen.c,v 1.8 1995/02/14 16:51:22 espie Exp espie $")
  24.  
  25. /*
  26. ###   CSupport/popen
  27. ###
  28. ###   NAME
  29. ###      popen/pclose -- Unix-like pipes
  30. ###
  31. ###   STATUS
  32. ###      Experimental
  33. ###      does not work with csh !
  34. ###
  35.  */
  36. FILE *popen(char *command, char *mode)
  37.    {
  38.    static char pname[25];
  39.    struct Task *me = FindTask(0);
  40.    static count = 0;
  41.    
  42.    count++;
  43.    
  44.       /* guarantees a unique pipe name ! */
  45.    sprintf(pname, "pipe:tr_%lx_%d", me, count);
  46.    
  47.    if (strcmp(mode, "r") == 0)
  48.       /* open pipe for reading */
  49.       {
  50.       FILE *reader;
  51.       BPTR writer, null;
  52.  
  53.       writer = Open(pname, MODE_NEWFILE);
  54.       reader = fopen(pname, "r");
  55.       null = Open("NIL:", MODE_NEWFILE);
  56.       if (SystemTags(command, SYS_Input, null, 
  57.          SYS_Output, writer, SYS_Asynch, TRUE, 
  58.          NP_StackSize, me->tc_SPUpper - me->tc_SPLower,
  59.          TAG_END) == -1)
  60.          {
  61.          Close(null);
  62.          Close(writer);
  63.          fclose(reader);
  64.          return NULL;
  65.          }
  66.       else
  67.          return reader;
  68.       }
  69.    else if (strcmp(mode, "w") == 0)
  70.       /* open pipe for writing */
  71.       {
  72.       FILE *writer;
  73.       BPTR reader, null;
  74.       
  75.       writer = fopen(pname, "w");
  76.       reader = Open(pname, MODE_OLDFILE);
  77.       null = Open("NIL:", MODE_NEWFILE);
  78.       if (SystemTags(command, SYS_Input, reader, 
  79.          SYS_Output, null, SYS_Asynch, TRUE, 
  80.          NP_StackSize, me->tc_SPUpper - me->tc_SPLower, 
  81.          TAG_END) == -1)
  82.          {
  83.          Close(null);
  84.          Close(reader);
  85.          fclose(writer);
  86.          return NULL;
  87.          }
  88.       else
  89.          return writer;
  90.       }
  91.    else
  92.       return NULL;
  93.    }
  94.  
  95. /* for us, pclose is just fclose.
  96.  * But we have to insure the file is empty first
  97.  */
  98. void pclose(FILE *f)
  99.    {
  100.    while (fgetc(f) != EOF)
  101.       ;
  102.    fclose(f);
  103.    }
  104.  
  105.